﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;


namespace HotelResSystem2
{
    public class ServicesFactory
    {
        private static ServicesFactory ServicesFactoryInstance;
        static readonly object padlock = new object();
        private ServicesFactory()
        {
        }
        public static ServicesFactory GetInstance()
        {
            lock (padlock)
            {
                if (ServicesFactoryInstance == null)
                {
                    ServicesFactoryInstance = new ServicesFactory();
                }
                return ServicesFactoryInstance;
            }
        }


        private CreditInstitutionAdapter CreditAdapter;

        Dictionary<string, CreditInstitutionAdapter> agencies = new Dictionary<string, CreditInstitutionAdapter>();

        private void setupDictionary()
        {
            if (!agencies.ContainsKey(BankOfAmericaCreditAdapter.ADAPTER_NAME))
            {
                BankOfAmericaCreditAdapter BoA = new BankOfAmericaCreditAdapter();
                agencies.Add(BankOfAmericaCreditAdapter.ADAPTER_NAME, BoA);
            }

            if (!agencies.ContainsKey(ChaseCreditAdapter.ADAPTER_NAME))
            {
                ChaseCreditAdapter chase = new ChaseCreditAdapter();
                agencies.Add(ChaseCreditAdapter.ADAPTER_NAME, chase);
            }

            if (!agencies.ContainsKey(RobinsFederalCreditAdapter.ADAPTER_NAME))
            {
                RobinsFederalCreditAdapter robins = new RobinsFederalCreditAdapter();
                agencies.Add(RobinsFederalCreditAdapter.ADAPTER_NAME, robins);
            }
        }


        public CreditInstitutionAdapter getCreditAdapter()
        {
            this.setupDictionary();

            if (CreditAdapter == null)
            {
                string className = DataStorage.GetInstance().getClassName();
                agencies.TryGetValue(className, out CreditAdapter);
            }

            return CreditAdapter;
        }
    }
}
